/*
 * @Author: 夏郑兴 971348024@qq.com
 * @Date: 2021-12-06 11:42:09
 * @LastEditors: 夏郑兴 971348024@qq.com
 * @LastEditTime: 2022-06-23 19:56:48
 * @FilePath: /gitee-project/Users/xzx/Desktop/A-xichen/yingxuan-git-project/pa220027-fenfa/src/utils/index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 前缀
// https://fenfa-vue-web.dokku2.yingxuan.co
export const prefix = import.meta.env.MODE === 'development' ? '' : window.location.origin
// oss 文件模板地址
export const ossPrefix = 'https://task-assignment.oss-cn-beijing.aliyuncs.com/static/import_template/'

export const ossPrefixNew = 'https://task-assignment.oss-cn-beijing.aliyuncs.com/static/import_template_new/'

// 格式化时间
export function dateChangeFormat(format: String, date: String) {
  date = new Date(date);
  const dataItem = {
    'Y+': date.getFullYear().toString(),
    'm+': (date.getMonth() + 1).toString(),
    'd+': date.getDate().toString(),
    'H+': date.getHours().toString(),
    'M+': date.getMinutes().toString(),
    'S+': date.getSeconds().toString(),
  };
  Object.keys(dataItem).forEach((item) => {
    const ret = new RegExp(`(${item})`).exec(format);
    if (ret) {
      format = format.replace(ret[1], ret[1].length === 1 ? dataItem[item] : dataItem[item].padStart(ret[1].length, '0'));
    }
  });
  return format;
}


export function dateFormat(date, format = 'yyyy-MM-dd HH:mm:ss') {
  date = new Date(date);
  var o = {
    'M+': date.getMonth() + 1, //month
    'd+': date.getDate(), //day
    'H+': date.getHours(), //hour
    'm+': date.getMinutes(), //minute
    's+': date.getSeconds(), //second
    'q+': Math.floor((date.getMonth() + 3) / 3), //quarter
    'S': date.getMilliseconds() //millisecond
  };
  if (/(y+)/.test(format))
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));

  for (var k in o)
    if (new RegExp('(' + k + ')').test(format))
      format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));

  return format;
}

/**
 * @author
 * @description 10位时间戳转换
 * @param time
 * @returns {string}
 */
export function tenBitTimestamp(time) {
  const date = new Date(time * 1000)
  const y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? '' + m : m
  let d = date.getDate()
  d = d < 10 ? '' + d : d
  let h = date.getHours()
  h = h < 10 ? '0' + h : h
  let minute = date.getMinutes()
  let second = date.getSeconds()
  minute = minute < 10 ? '0' + minute : minute
  second = second < 10 ? '0' + second : second
  return y + '年' + m + '月' + d + '日 ' + h + ':' + minute + ':' + second //组合
}

// 计算两个时间之间的时间差 多少天时分秒
export function intervalTime(startTime, endTime) {
  // var timestamp=new Date().getTime(); //计算当前时间戳
  var timestamp = (Date.parse(new Date())) / 1000;//计算当前时间戳 (毫秒级)
  var date1 = ""; //开始时间
  if (timestamp < startTime) {
    date1 = startTime;
  } else {
    date1 = timestamp; //开始时间
  }
  var date2 = endTime; //结束时间
  // var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
  var date3 = (date2 - date1) * 1000; //时间差的毫秒数
  //计算出相差天数
  var days = Math.floor(date3 / (24 * 3600 * 1000));
  //计算出小时数

  var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000));
  //计算相差分钟数
  var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000));

  //计算相差秒数

  var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  var seconds = Math.round(leave3 / 1000);
  console.log(days + "天 " + hours + "小时 ")
  return days + "天 " + hours + ":" + minutes + ":" + seconds
  // return   days + "天 " + hours + "小时 "
}

export function calculateDiffTime(startTime: Number, endTime: Number) {
  // let startTime = '1629107469000'  //2021-08-16 17:51
  // let endTime = '1976262719000'  //2032-08-16 17:51
  let runTime = parseInt((endTime - startTime) / 1000);
  var year = Math.floor(runTime / 86400 / 365);
  runTime = runTime % (86400 * 365);
  var month = Math.floor(runTime / 86400 / 30);
  runTime = runTime % (86400 * 30);
  var day = Math.floor(runTime / 86400);
  runTime = runTime % 86400;
  var hour = Math.floor(runTime / 3600);
  runTime = runTime % 3600;
  var minute = Math.floor(runTime / 60);
  runTime = runTime % 60;
  var second = runTime;
  console.log(`相差${year}年${month}月${day}天${hour}小时${minute}分${second}秒`);
  if (year > 0) {
    day = day + (day * 365)
  }
  if (month > 0) {
    day = day + (month * 30)
  }
  if (day > 0) {
    hour = hour + (day * 24)
  }
  // if (day < 10) {
  //   day = '0' + day
  // }
  if (hour < 10) {
    hour = '0' + hour
  }
  if (minute < 10) {
    minute = '0' + minute
  }
  if (second < 10) {
    second = '0' + second
  }

  return hour + ':' + minute + ':' + second;
  // return year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + second;
}